/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.listeners.automation
{
	import funit.core.ITestListener;
	import funit.core.TestName;
	import funit.core.TestOutput;
	import funit.core.TestResult;
	import funit.framework.MessageString;
	import funit.framework.MessageUtils;
	import funit.writers.DebugWriter;
	import funit.writers.IWriter;
	
	public class TeamCityTestListener implements ITestListener
	{
		private var _writer:IWriter;
		
		
		/**
		 * @param writer
		 */
		public function TeamCityTestListener( writer:IWriter = null )
		{
			this.writer = writer;
		}
		
		
		/**
		 * @param message
		 * @param messageParameters
		 */
		private function writeLine( message:String = "", ... messageParameters ) : void
		{
			var len:int = messageParameters.length;
			for (var i:int=0; i<len; i++)
			{
				messageParameters[i] = escapeSpecial(messageParameters[i]);
			}
			
			messageParameters.unshift(message);
			
			(_writer.writeLine as Function).apply(null, messageParameters);
		}
		
		/**
		 * @param value
		 * @return 
		 */
		private function escapeSpecial( value:String ) : String
		{
			return	value.replace(
						/[\|\]\r\n\']/g,
						replaceMethod
					);
		}
		
		/**
		 * @param char
		 * @param parameters
		 * @return 
		 */
		private function replaceMethod( char:String, ... parameters ) : String
		{
			switch(char)
			{
				case "\r":
					return "|r";
				case "\n":
					return "|n";
				case "'":
					return "|'";
				case "|":
					return "||";
				case "]":
					return "|]";
				default:
					return char;
			}
		}
		
		/**
		 * @param name
		 * @return 
		 */
		private function formatTestName( name:String ) : String
		{
			return name.replace("::", ".");
		}
		
		/**
		 * @param name
		 * @param testCount
		 */
		public function runStarted( name:String, testCount:int ) : void
		{
			
		}
		
		/**
		 * @param testResult
		 */
		public function runFinished( testResult:TestResult ) : void
		{
			
		}
		
		/**
		 * @param error
		 */
		public function runFinishedError( error:Error ) : void
		{
			
		}
		
		/**
		 * @param testName
		 */
		public function testStarted( testName:TestName ) : void
		{
			writeLine("##teamcity[testStarted name='{0}']", formatTestName(testName.fullName) );
		}
		
		/**
		 * @param testResult
		 */
		public function testFinished( testResult:TestResult ) : void
		{
			var message:String = MessageUtils.trimCharacter(testResult.message, MessageString.NewLine);
			
			if (testResult.executed)
			{
				if (testResult.isFailure)
				{
					var details:String = message + MessageString.NewLine + testResult.stackTrace;
					var failureType:String = testResult.failureSite.value;
					
					writeLine(
						"##teamcity[testFailed name='{0}' message='{1}' details='{2}' type='{3}']",
						formatTestName(testResult.name),
						message,
						details,
						failureType
					);
				}
				
				writeLine("##teamcity[testFinished name='{0}' duration='{1}']", formatTestName(testResult.name), testResult.time );
			}
			else
			{
				writeLine("##teamcity[testIgnored name='{0}' message='{1}']", formatTestName(testResult.name), message );
			}
		}
		
		/**
		 * @param suiteName
		 */
		public function suiteStarted( suiteName:TestName ) : void
		{
			writeLine("##teamcity[testSuiteStarted name='{0}']", formatTestName(suiteName.fullName) );
		}
		
		/**
		 * @param suiteResult
		 */
		public function suiteFinished( suiteResult:TestResult ) : void
		{
			writeLine("##teamcity[testSuiteFinished name='{0}']", formatTestName(suiteResult.name) );
		}
		
		/**
		 * @param error
		 */
		public function unhandledError( error:Error ) : void
		{
			
		}
		
		/**
		 * @param testOutput
		 */
		public function testOutput( testOutput:TestOutput ) : void
		{
			
		}
		
		
		/**
		 * 
		 */
		public function get writer() : IWriter
		{
			return _writer;
		}
		
		public function set writer( value:IWriter ) : void
		{
			if (value != null)
			{
				_writer = value;
			}
			else
			{
				_writer = new DebugWriter();
			}
		}
		
	}
	
}
